from xlwings import *
import xlwings as xw

#5 单元格
app3 = xw.App(visible=True, add_book=False)
app3.screen_updating = False
app3.display_alerts = False
filepath3 = './工作簿1.xlsx'
wb3 = app3.books.open(filepath3)
sht3 = wb3.sheets('Sheet1')

# range1 = sht3.range('A1')
# range1.offset(row_offset=5, column_offset=6)
# range1.expand(mode='down')  # 'down','right','table'右下方
# range1.resize(row_size=4, column_size=2)
# range1.current_region   # 全选

# 存储数据
# 存储在A1:C1中
sht3.range('A1').value = [1, 2, 3]
# 存储在A1:A3中
sht3.range('A1').options(transpose=True).value = [4, 5, 6]
# 二维数据存储在A1:B2中
sht3.range('A1').options(expand='table').value = [[1, 2], [3, 4]]

# 读取数据
lst3 = sht3.range('A1:B2').value
print(lst3)
row_max = sht3.range('A1').end('down').row
col_max = sht3.range('A1').end('right').column
lst4 = sht3.range((1, 1), (row_max, col_max)).value
print(lst4)

# 清除与删除
range3 = sht3.range('A1')
range3.clear_contents()
range3.clear()
range3.delete(shift=None)

# 格式及其他设置
# 合并
sht3.range('B2:C2').merge() # unmerge取消合并
# 字体设置
range4 = sht3.range('B1')
range4.Font.Color = (0, 255, 255)
# range4.Font.Color = 0xffff00
range4.Font.Size = 25
range4.Font.Bold = True


wb3.save()
wb3.close()
app3.quit()